# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openexr

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.0
pkgrel=6
pkgdesc="Openexr library for EXR images (mingw-w64)"
arch=('any')
url="http://http://www.openexr.com/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-ilmbase")
options=('staticlibs' 'strip')
source=("http://download.savannah.nongnu.org/releases/openexr/${_realname}-${pkgver}.tar.gz"
        mingw-w64-fix.patch
        openexr-2.1.0-headers.patch
        openexr-2.1.0_bb44ExpLogTable.patch
        openexr-2.1.0_aligned-malloc.patch
        openexr-2.1.0_cast.patch
        openexr_obsolete-macros.patch
        cmake-soversion.patch
        pull87.patch
        pull93.patch)
md5sums=('33735d37d2ee01c6d8fbd0df94fb8b43'
         '346e4c67b11e4f688cb3e751604342e0'
         'd87df3d057dd8d902c61a3b88f05fd0d'
         '4798df032716e33a2b227542319fa8f5'
         '09df5525cf2d53d013304dd5e533ce0f'
         '4ec9ab6cc823dc9fcc28cfc85a761546'
         '2f20a165ea31e7638a37809dd5feefb1'
         '8ced9edee3f9c07ecfbb865b2c0419f3'
         'cf2a01f644ef03b37dd7d7a22cb21cb6'
         'd674128daaa2b66fe13561e64f383fb7')

prepare(){
  cd "$srcdir/${_realname}-$pkgver"
  
  sed -i 's/#define ZLIB_WINAPI/\/\/#define ZLIB_WINAPI/g' IlmImf/ImfZipCompressor.cpp
  sed -i 's/#define ZLIB_WINAPI/\/\/#define ZLIB_WINAPI/g' IlmImf/ImfPxr24Compressor.cpp

  patch -Np1 -i "$srcdir/mingw-w64-fix.patch"
  patch -Np1 -i "$srcdir/openexr-2.1.0-headers.patch"
  patch -Np1 -i "$srcdir/openexr-2.1.0_bb44ExpLogTable.patch"
  patch -Np1 -i "$srcdir/openexr-2.1.0_aligned-malloc.patch"
  patch -Np1 -i "$srcdir/openexr-2.1.0_cast.patch"
  patch -Np1 -i "$srcdir/openexr_obsolete-macros.patch"
  patch -Np1 -i "$srcdir/cmake-soversion.patch"
  patch -Np2 -i "$srcdir/pull87.patch"
  patch -Np2 -i "$srcdir/pull93.patch"

  #./bootstrap
}

build() {
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/OpenEXR"
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${pkgdir}${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_VERBOSE_MAKEFILE=ON \
    -DUSE_ZLIB_WINAPI=OFF \
    ../${_realname}-${pkgver}

  #-DCMAKE_SHARED_LINKER_FLAGS="-Wl,--export-all-symbols"
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install

  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/OpenEXR.pc
}
